﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace Confessor
{
    static class xNetworkDriveFunctions
    {
        // Map the drives
        public static string mapNetworkDriveToTarget(string userName, string password, string targetHost)
        {
            string mappedDriveLetter = string.Empty;

            // Get the drive letters already used
            List<string> driveList = new List<string>();
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo d in drives)
            {
                driveList.Add(d.Name.Trim(':','\\'));
            }

            //Set the drive to map to the first available drive letter after 'E'.
            char[] potentialDriveLetters = { 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                               'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            foreach (char c in potentialDriveLetters)
            {
                bool driveMatch = false;
                foreach (string s in driveList)
                {
                    if (c.ToString().Equals(s))
                    {
                        driveMatch = true;
                    }
                 }
                if (!driveMatch)
                {
                    mappedDriveLetter = c.ToString();
                    break;
                }
            }
            
            // Map the drive
            if (mappedDriveLetter != string.Empty)
            {
                try
                {
                    ProcessStartInfo p = new ProcessStartInfo(@"net.exe", @" use " + mappedDriveLetter + @": \\" + targetHost + @"\c$ /USER:" + userName + " " + password);
                    Process mapDriveProc = new Process();
                    mapDriveProc.StartInfo = p;
                    mapDriveProc.Start();
                    mapDriveProc.WaitForExit();
                    // Verify the command worked.
                    {
                        driveList.Clear();
                        DriveInfo[] currentDrives = DriveInfo.GetDrives();
                        foreach (DriveInfo d in currentDrives)
                        {
                            driveList.Add(d.Name.Trim(':', '\\'));
                        }
                        bool driveWasMapped = false;
                        foreach (string s in driveList)
                        {
                            if(s.Equals(mappedDriveLetter))
                            {
                                driveWasMapped = true;
                            }
                        }
                        if (driveWasMapped == true)
                        {
                            return mappedDriveLetter;
                        }
                        else
                        {
                            return mappedDriveLetter = targetHost + ": Drive not mapped for the target host.";
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Caller, needs to handle the errors gracefully and write the error to a error log...
                    // XFileActions xFA = new XFileActions();
                    // xFA.WriteToErrorLog(statusMessage, scanObject);
                    string statusMessage = targetHost + ": " + ex.Message;
                    return statusMessage;
                }
            }
            else
            {
                return targetHost + ": No drive letters available, could not map a network drive to target host";
            }
        }

        public static string removeMappedDrive(string driveLetter)
        {
            try
            {
                ProcessStartInfo p = new ProcessStartInfo(@"net.exe", @" use " + driveLetter + @": /delete");
                Process removeMappedDriveProc = new Process();
                removeMappedDriveProc.StartInfo = p;
                removeMappedDriveProc.Start();
                removeMappedDriveProc.WaitForExit();
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
